/*
 * Package il.ac.biu.cs.grossmm.test.cpptest
 * File FooToValueConverter.java
 * 
 * This is a part of presence service framework API. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package il.ac.biu.cs.grossmm.tests.cpptest;

import static il.ac.biu.cs.grossmm.api.data.NodeTypeByInterface.*;
import static il.ac.biu.cs.grossmm.api.data.NodeOf.*;
import il.ac.biu.cs.grossmm.api.OperationFailedException;
import il.ac.biu.cs.grossmm.api.data.Root;
import il.ac.biu.cs.grossmm.api.data.NodeOf;
import il.ac.biu.cs.grossmm.api.flow.ConverterBase;
import il.ac.biu.cs.grossmm.api.flow.ConverterProxy;
import il.ac.biu.cs.grossmm.api.flow.NodeEventInspector;
import il.ac.biu.cs.grossmm.api.flow.NodeInspector;
import il.ac.biu.cs.grossmm.api.flow.NodeManipulator;

public class FooToValueConverter extends
	ConverterBase<FooPresence, NodeOf<FooPresenceValue>> {

	public FooToValueConverter()
	{
		super("Presence To Value Converter", nodeType(FooPresence.class),
			nodeOf(FooPresenceValue.class), 0.9);

	}

	public ConverterProxy<FooPresence, NodeOf<FooPresenceValue>> createProxy(
		Root<FooPresence> srcNode, Root<NodeOf<FooPresenceValue>> dstNode, 
		NodeInspector srcInspector,
		NodeManipulator dstManipulator) throws OperationFailedException
	{
		return new Proxy(srcInspector, dstManipulator, srcNode, dstNode);
	}

	class Proxy implements
		ConverterProxy<FooPresence, NodeOf<FooPresenceValue>> {
		Root<FooPresence> srcNode;

		Root<NodeOf<FooPresenceValue>> dstNode;

		Proxy(NodeInspector srcInspector, NodeManipulator dstManipulator, 
			Root<FooPresence> srcNode,
			Root<NodeOf<FooPresenceValue>> dstNode) throws OperationFailedException
		{
			this.srcNode = srcNode;
			this.dstNode = dstNode;
			convert2(srcInspector, dstManipulator);
		}

		public void convert(NodeEventInspector srcInspector,
			NodeManipulator dstManipulator) throws OperationFailedException
		{
			convert2(srcInspector, dstManipulator);
		}
		
		public void convert2(NodeInspector srcInspector,
			NodeManipulator dstManipulator) throws OperationFailedException
		{
			String entity = srcInspector.getValue(srcNode, FooPresence.ENTITY);
			boolean online =
				srcInspector.getValue(srcNode, FooPresence.IS_ONLINE);

			FooPresenceValue value = new FooPresenceValue(entity, online);

			dstManipulator.setValue(dstNode, property(FooPresenceValue.class),
				value);
		}

		public void release()
		{
		}
	}
}
